package com.wannengbang.cloudleader.homepage;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.DeviceVipTradeBean;
import com.wannengbang.cloudleader.bean.QueueRewardListBean;
import com.wannengbang.cloudleader.bean.ShopBindBean;
import com.wannengbang.cloudleader.dialog.AwardRuleDialog;
import com.wannengbang.cloudleader.homepage.adapter.NoticeAwardListAdapter;
import com.wannengbang.cloudleader.homepage.adapter.NoticeDynamicListAdapter;
import com.wannengbang.cloudleader.homepage.adapter.RankingAwardListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.shop.BindingMachinesActivity;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.AutoScrollRecyclerView;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商户奖励
 */
public class BountyListActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_transaction)
    TextView tvTransaction;
    @BindView(R.id.tv_award)
    TextView tvAward;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.recyclerView_dynamic)
    AutoScrollRecyclerView recyclerViewDynamic;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_look_rule)
    TextView tvLookRule;
    @BindView(R.id.recyclerView_award)
    RecyclerView recyclerViewAward;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.tv_rank_top2)
    TextView tvRankTop2;
    @BindView(R.id.tv_rank_top_money2)
    TextView tvRankTopMoney2;
    @BindView(R.id.tv_rank_top1)
    TextView tvRankTop1;
    @BindView(R.id.tv_rank_top_money1)
    TextView tvRankTopMoney1;
    @BindView(R.id.tv_rank_top3)
    TextView tvRankTop3;
    @BindView(R.id.tv_rank_top_money3)
    TextView tvRankTopMoney3;
    @BindView(R.id.ll_ranking_bottom)
    LinearLayout llRankingBottom;
    @BindView(R.id.iv_award_title4)
    ImageView ivAwardTitle4;
    @BindView(R.id.iv_award_title5)
    ImageView ivAwardTitle5;
    private NoticeDynamicListAdapter noticeDynamicListAdapter;
    private NoticeAwardListAdapter noticeAwardListAdapter;
    private RankingAwardListAdapter rankingAwardListAdapter;
    private HomePageModelImpl homePageModel;
    private int type = 1;
    private List<QueueRewardListBean.DataBean.ItemListBean> noticeDynamicList;
    private List<QueueRewardListBean.DataBean.ItemListBean> noticeAwardList;
    private List<QueueRewardListBean.DataBean.ItemListBean> rankingAwardList;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bounty_list);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();

        noticeDynamicList = new ArrayList<>();
        noticeDynamicListAdapter = new NoticeDynamicListAdapter(noticeDynamicList);
        noticeDynamicListAdapter.setType(type);
        recyclerViewDynamic.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerViewDynamic.setAdapter(noticeDynamicListAdapter);

        noticeAwardList = new ArrayList<>();
        noticeAwardListAdapter = new NoticeAwardListAdapter(noticeAwardList);
        recyclerViewAward.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerViewAward.setAdapter(noticeAwardListAdapter);

        rankingAwardList = new ArrayList<>();
        rankingAwardListAdapter = new RankingAwardListAdapter(rankingAwardList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerView.setAdapter(rankingAwardListAdapter);

        requestDeviceVipTrade();
        requestDeviceSchedule();
        requestDeviceQueueRewardTrend();
        requestDeviceQueueReward();
        requestDeviceLeaderboard();
        requestBindIndex();
    }

    public void requestBindIndex() {
        new ShopModelImpl().requestIntegralBindIndex(new DataCallBack<ShopBindBean>() {
            @Override
            public void onSuccessful(ShopBindBean bean) {
                if (bean.getData() == null || bean.getData().size() == 0) {
                    dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                    dialogBuild.setMessage("请先绑定VIP机具，绑定后刷卡奖励");
                    dialogBuild.setPositiveVersionButton("去绑定", v -> {
                        dialogBuild.dismiss();
                        dialogBuild = null;
                        showActivity(BindingMachinesActivity.class);
                    });
                    dialogBuild.setNegativeButton("取消", v -> {
                        dialogBuild.dismiss();
                        dialogBuild = null;
                    });
                    dialogBuild.setCancelable(false);
                    dialogBuild.show();
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 交易量
     */
    public void requestDeviceVipTrade() {
        homePageModel.requestDeviceVipTrade(new DataCallBack<DeviceVipTradeBean>() {
            @Override
            public void onSuccessful(DeviceVipTradeBean bean) {
                if (bean.getData() != null) {
                    String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getAll_money()) / 100);
                    tvAllMoney.setText(money);
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 开奖进度
     */
    private void requestDeviceSchedule() {
        homePageModel.requestDeviceSchedule(type, new DataCallBack<DeviceVipTradeBean>() {
            @Override
            public void onSuccessful(DeviceVipTradeBean bean) {
                progressbar.setProgress(bean.getData().getSchedule());
                tvProgress.setText(bean.getData().getSchedule() + "%");
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 动态
     */
    private void requestDeviceQueueRewardTrend() {
        homePageModel.requestDeviceQueueRewardTrend(type, 1, new DataCallBack<QueueRewardListBean>() {
            @Override
            public void onSuccessful(QueueRewardListBean bean) {
                noticeDynamicList.clear();
                if (bean.getData().getItemList().size() > 0) {
                    noticeDynamicList.addAll(bean.getData().getItemList());
                }
                noticeDynamicListAdapter.setType(type);
                noticeDynamicListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 我的排队
     */
    private void requestDeviceQueueReward() {
        homePageModel.requestDeviceQueueReward(type, 1, new DataCallBack<QueueRewardListBean>() {
            @Override
            public void onSuccessful(QueueRewardListBean bean) {
                noticeAwardList.clear();
                if (bean.getData().getItemList().size() > 0) {
                    noticeAwardList.addAll(bean.getData().getItemList());
                }
                noticeAwardListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 排行榜
     */
    private void requestDeviceLeaderboard() {
        homePageModel.requestDeviceLeaderboard(type, 1, new DataCallBack<QueueRewardListBean>() {
            @Override
            public void onSuccessful(QueueRewardListBean bean) {
                rankingAwardList.clear();
                llRankingBottom.setVisibility(View.VISIBLE);
                tvRankTop1.setText("暂无");
                tvRankTopMoney1.setText("0");
                tvRankTop2.setText("暂无");
                tvRankTopMoney2.setText("0");
                tvRankTop3.setText("暂无");
                tvRankTopMoney3.setText("0");
                if (bean.getData().getItemList().size() > 0) {
                    llRankingBottom.setVisibility(View.GONE);
                    if (bean.getData().getItemList().size() == 1) {
                        tvRankTop1.setText(bean.getData().getItemList().get(0).getName());
                        String money1 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(0).getMoney()) / 100);
                        tvRankTopMoney1.setText(money1);
                    } else if (bean.getData().getItemList().size() == 2) {
                        tvRankTop1.setText(bean.getData().getItemList().get(0).getName());
                        String money1 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(0).getMoney()) / 100);
                        tvRankTopMoney1.setText(money1);

                        tvRankTop2.setText(bean.getData().getItemList().get(1).getName());
                        String money2 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(1).getMoney()) / 100);
                        tvRankTopMoney2.setText(money2);
                    } else if (bean.getData().getItemList().size() == 3) {
                        tvRankTop1.setText(bean.getData().getItemList().get(0).getName());
                        String money1 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(0).getMoney()) / 100);
                        tvRankTopMoney1.setText(money1);

                        tvRankTop2.setText(bean.getData().getItemList().get(1).getName());
                        String money2 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(1).getMoney()) / 100);
                        tvRankTopMoney2.setText(money2);

                        tvRankTop3.setText(bean.getData().getItemList().get(2).getName());
                        String money3 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(2).getMoney()) / 100);
                        tvRankTopMoney3.setText(money3);
                    } else {
                        tvRankTop1.setText(bean.getData().getItemList().get(0).getName());
                        String money1 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(0).getMoney()) / 100);
                        tvRankTopMoney1.setText(money1);

                        tvRankTop2.setText(bean.getData().getItemList().get(1).getName());
                        String money2 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(1).getMoney()) / 100);
                        tvRankTopMoney2.setText(money2);

                        tvRankTop3.setText(bean.getData().getItemList().get(2).getName());
                        String money3 = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getItemList().get(2).getMoney()) / 100);
                        tvRankTopMoney3.setText(money3);
                        for (int i = 3; i < bean.getData().getItemList().size(); i++) {
                            rankingAwardList.add(bean.getData().getItemList().get(i));
                        }
                        llRankingBottom.setVisibility(View.VISIBLE);
                    }
                } else {
                    llRankingBottom.setVisibility(View.GONE);
                }
                rankingAwardListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onStart() {
        super.onStart();
        //流式滚动效果
        recyclerViewDynamic.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        recyclerViewDynamic.stop();
    }

    @OnClick({R.id.tv_transaction, R.id.tv_award, R.id.tv_look_rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_transaction:
                tvAward.setTextColor(Color.parseColor("#ffffff"));
                tvAward.setBackgroundResource(R.drawable.shape_rectangle_conner_yellow4);
                tvTransaction.setTextColor(Color.parseColor("#FF9E02"));
                tvTransaction.setBackgroundResource(R.drawable.shape_rectangle_conner_white4);
                ivAwardTitle4.setImageResource(R.mipmap.ic_award_title4);
                ivAwardTitle5.setImageResource(R.mipmap.ic_award_title5);
                type = 1;
                requestDeviceVipTrade();
                requestDeviceSchedule();
                requestDeviceQueueRewardTrend();
                requestDeviceQueueReward();
                requestDeviceLeaderboard();
                break;
            case R.id.tv_award:
                tvTransaction.setTextColor(Color.parseColor("#ffffff"));
                tvTransaction.setBackgroundResource(R.drawable.shape_rectangle_conner_yellow4);
                tvAward.setTextColor(Color.parseColor("#FF9E02"));
                tvAward.setBackgroundResource(R.drawable.shape_rectangle_conner_white4);
                ivAwardTitle4.setImageResource(R.mipmap.ic_award_title4_2);
                ivAwardTitle5.setImageResource(R.mipmap.ic_award_title5_2);
                type = 2;
                requestDeviceVipTrade();
                requestDeviceSchedule();
                requestDeviceQueueRewardTrend();
                requestDeviceQueueReward();
                requestDeviceLeaderboard();
                break;
            case R.id.tv_look_rule:
                AwardRuleDialog dialog = AwardRuleDialog.newInstance();
                dialog.show(getSupportFragmentManager(), "dialog");
                break;
        }
    }
}
